﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using HanGu.Common;

namespace Hangu.WebClient.Controllers
{
    public class DownLoadController : VipBaseController
    {
        //
        // GET: /DownLoad/

        public void Index(string id)
        {
            string path = id;
            path = HanGu.Common.RijndaelSecurity.DecryptDES(path);
            string fullpath = Server.MapPath(path);
            if (System.IO.File.Exists(fullpath))
            {
                FileInfo file = new FileInfo(fullpath);
                Response.Clear();//清除所有缓存区的内容  
                //Response.Charset = "GB2312";//定义输出字符集
                Response.ContentEncoding = Encoding.Default;//输出内容的编码为默认编码 
                Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);//添加头信息。为“文件下载/另存为”指定默认文件名称    
                Response.AddHeader("Content-Length", file.Length.ToString());//添加头文件，指定文件的大小，让浏览器显示文件下载的速度   
                Response.WriteFile(file.FullName);// 把文件流发送到客户端    
                Response.End();//将当前所有缓冲区的输出内容发送到客户端，并停止页面的执行
            }

        }

        [HttpPost]
        public string SaveFile(FormCollection collection)
        {
            //string guid = Request["guid"];
            string classType = Request["ClassType"];
            //string groupName = Request["GroupName"];            
            using (UploadFileManage uploadFile = new UploadFileManage(Request.Files[0]))
            {
                var img = System.Drawing.Image.FromStream(Request.Files[0].InputStream);
                uploadFile.ClassType = classType;
                uploadFile.Save();
                if (!uploadFile.Error)
                {
                    string path = uploadFile.FileInfo["filepath"];
                    ajaxStr = string.Format(ajaxStr + ",\"width\":\"{5}\",\"height\":\"{6}\"", 200, ResourceManager.SaveSuccess, navTabId, path, "", img.Width,img.Height);
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }
            }
            return "{" + ajaxStr + "}";
        }
        //
        // GET: /DownLoad/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /DownLoad/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /DownLoad/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /DownLoad/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /DownLoad/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /DownLoad/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /DownLoad/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
